package se.webbzon.boltzmann.game.object.waypoint;

import java.nio.ByteBuffer;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.oschi01.friendly.Friendly;
import se.webbzon.oschi01.worldeditor.WorldMovableObjectDeflation;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;

public class TargetDeflation extends WorldMovableObjectDeflation {
	
	private PathProgramEnum program;
	
	private int time, duration;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public TargetDeflation(int deflationProperties, WorldObject[] dependencies) {
		super(deflationProperties, dependencies);
	}

	public TargetDeflation(int deflationProperties, Target target) {
		super(	deflationProperties, 
				target.getWaypoint() == null ? null : new WorldObject[] {target.getWaypoint()}, 
				target);
		program = target.getProgram();
		time = target.pos();
		duration = target.getCycleDuration();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void inflate(ByteBuffer buff) {
		super.inflate(buff);
		program = PathProgramEnum.values()[buff.getInt()];
		time = buff.getInt();
		duration = buff.getInt();
	}

	@Override public ByteBuffer deflate(int allocate) {
		ByteBuffer buff = super.deflate(allocate + 12);
		buff.putInt(program.ordinal());
		buff.putInt(time);
		buff.putInt(duration);
		return buff;
	}
	
	/*============================================================
	Friendly Methods
	============================================================*/
	
	@Friendly WorldStaticObject makeTarget(AbstractEnvironment env) {
		WorldStaticObject instance;
		Waypoint waypoint = dependencies() > 0 ? 
				(Waypoint) getDependency(0) : null;
		
		Target target = new Target(env,program,waypoint);
		target.setCycleDuration(duration);
		target.seek(time);
		instance = target;
		
		instance.setPosition(getX(),getY(),getZ());
		instance.setSize(getWidth(), getHeight(), getDepth());
		instance.setRotation(getEX(), getEY(), getEZ(), getAngle());
		
		return instance;
	}

}
